<?php

namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseData;
use App\Helpers\Untils;
use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRegisterRequest;
use App\Models\SystemUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
class SystemUserController extends Controller
{
    use ResponseData,Untils;

    //账号列表
    public function list(SystemUser $model,Request $request){
        $size = $request->input('size',10);
        $user = $request->user();
        $model = $model->with('roles','department','first','two','three');
        if($user->department_one_id!=1){
            $lastDepartment = $this->findLastDepartment($user);
            $model = $model->where('department_one_id',$user->department_one_id);
            $model = $model->where('department_id','>=',$lastDepartment);
        }
        if($request->filled('name')){
            $model = $model->where('name','like','%'.$request->name.'%');
        }
        if($request->filled('username')){
            $model = $model->where('username','like','%'.$request->username.'%');
        }
        if($request->filled('phone')){
            $model = $model->where('phone','like','%'.$request->phone.'%');
        }
        if($request->filled('department_id')){
            $model = $model->where('department_id',$request->department_id);
        }
        if($request->filled('status')){
            $model = $model->where('status',$request->status);
        }
        if($request->filled('is_author')){
            $model = $model->where('is_author',$request->is_author);
        }
        if($request->type==2){
            $data = $model->where('username','<>','superadmin')
                ->select('name','id')
                ->orderByDesc('id')->get();
        }else{
            $data = $model->where('username','<>','superadmin')
                ->orderByDesc('id')->paginate($size);
        }

        return $this->success($data);
    }
    //创建账号
    public function create(LoginRegisterRequest $request,SystemUser $user){
        $data = $request->only('name','username','phone','operator_id','operator_name','is_author','department_id','data_permission');
        $data['department_one_id']=$request->input('department_one_id',0);
        $data['department_two_id']=$request->input('department_two_id',0);
        $data['department_three_id']=$request->input('department_three_id',0);
        //$data['data_permission'] = $this->findLastDepartment($request);
        $data['password']=Hash::make($request->password);
        $data['last_login_ip'] = $request->ip();
        $res = $user->query()->create($data);
        return $this->success($res);
    }

    public function update(SystemUser $model,Request $request){
        $data = $model->find($request->id);
        $request->except('password');
        $res = $request->all();
        if($request->filled('rate')){
            $res['rate_at'] = date("Ym",time());
        }
        $data->update($res);
        if($request->status==2){
            $data->tokens()->delete();
        }
        return $this->success('修改成功');
    }
    //修改密码
    public function updatePassword(SystemUser $user,Request $request){
        $admin = $request->user();
//        if($admin->username=='superadmin'){
            $user = $this->getUser($user)->where('username',$request->username)->first();
            if($user){
                $user->password = Hash::make($request->password);
                $user->save();
                $user->tokens()->delete();
                return $this->success('密码修改成功');
            }else{
                return $this->failed('当前用户不存在',201);
            }
//        }else{
//            return $this->failed('非法用户',201);
//        }
    }

    //编辑
    public function edit(SystemUser $user,Request $request){
        // $data['role'] = Role::all();
        // $user = $this->getUser($user)->find($request->id);
        // $data['owner_roles'] = $user->getRoleNames();
        // return $this->success($data);
        $data = $user->find($request->id);
        return $this->success($data);
    }
    //用户添加角色
    public function role(Request $request)
    {
        $user = SystemUser::query()->find($request->id);
        $name = Role::query()->select('name')->find($request->role_id);
        $user->syncRoles($name->name);
        return $this->success('添加成功');
    }
    //删除角色
    public function deleteRole(Request $request,SystemUser $system){
        $user = $system->find($request->id);
//        $user = $request->user();
        //获取所有角色
        $user->syncRoles([]);
        return $this->success('更新成功');
    }
    //获取用户所有角色
    public function getRole(Request $request){
        $user = $request->user();
        $roles = $user->getRoleNames();
        $id = Role::query()->whereIn('name',$roles)->pluck('id');
        return $this->success($id);
    }
    private function getUser($user){
        return $user->query()->where('username','<>','superadmin');
    }
    //根据手机号查询用户
    public function getUserByPhone(Request $request){
        $user = SystemUser::query()->where('phone',$request->phone)->first();
        return $this->success($user);
    }
}
